﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Maknys.Consultator.DomainModel;
using Maknys.Consultator.DataContracts;
using System.Collections;

namespace Maknys.Consultator.Repositories
{
    public class TreeRepository : ITreeRepository
    {
        private ArrayList infoList = new ArrayList();
        private List<string> list = new List<string>();

        public TreeRepository() 
        {
        }

        public void MakeTree()
        {
        }
 
        public void Add(TreeNode<Element> root, Element element)
        {
            IEnumerable <TreeNode <Element> > searchList = root.GetEnumerable(TreeTraversalType.BreadthFirst, TreeTraversalDirection.TopDown);
          
            foreach (TreeNode<Element> node in searchList)
            {

                string id = node.Value.Id;
                string pId = element.PId;

                if (id.Equals(pId))
                {
                    node.Children.Add(element);
                }

            }

        }

        public List<string> GetList(TreeNode<Element> root, string sentence)
        {
            list.Clear();

            IEnumerable<TreeNode<Element>> searchList = root.GetEnumerable(TreeTraversalType.BreadthFirst, TreeTraversalDirection.TopDown);

            foreach (TreeNode<Element> node in searchList)
            {

                string value = node.Value.Value;

                if (value.Equals(sentence))
                {
                    GetChilds(node);
                    return list;
                }

            }

            return list;

        }

        public void GetChilds(TreeNode<Element> Subtree)
        {

            TreeNodeList<Element> serachNode = Subtree.Children;

            foreach (TreeNode<Element> node in serachNode)
            {

                Element nauj = node.Value;
                string value = nauj.Value;
                list.Add(value);

            }

        }

        public void FillTree(ArrayList infoList, TreeNode<Element> root)
        {

            foreach (Element inf in infoList)
            {

                IEnumerable<TreeNode<Element>> searchList = root.GetEnumerable(TreeTraversalType.BreadthFirst, TreeTraversalDirection.TopDown);

                foreach (TreeNode<Element> node in searchList)
                {

                    string idSearched = node.Value.Id;
                    string id = inf.PId;

                    if (idSearched.Equals(id))
                    {
                        node.Children.Add(inf);
                    }

                }

            }

        }

    }
}
